﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using DbService;
using System.Data;
using System.IO;
using antuAutoUPTools.Entries;
namespace antuAutoUPTools.Business
{
    public class XmlClass
    {
        static string tdzzbayw = "TdzzbaywConnString";
        static string tdzzba = "TdzzbaConnString";
        private static string ywStr = ConfigurationManager.ConnectionStrings["TdzzbaywConnString"].ToString();
        private static string tdzzStr=ConfigurationManager.ConnectionStrings["TdzzbaConnString"].ToString();
        DbService.Database db_tdzzyw = new DbService.Database(tdzzbayw);
        DbService.Database db_tdzz = new DbService.Database(tdzzba);
        public string bxt_username =ConfigurationManager.AppSettings["BXT_USERNAME"];
        private string bxt_password = ConfigurationManager.AppSettings["BXT_PASSWORD"];
        private string bxt_xzqbm = ConfigurationManager.AppSettings["BXT_XZQBM"];
        private string jhxmlpath = AppDomain.CurrentDomain.BaseDirectory + "Template\\JH.xml";
        private string ssxmlpath = AppDomain.CurrentDomain.BaseDirectory + "Template\\SS.xml";
        private string ysxmlpath = AppDomain.CurrentDomain.BaseDirectory + "Template\\YS.xml";
        private string qtgbzpath = AppDomain.CurrentDomain.BaseDirectory + "Template\\qtgbnt.xml";
        #region 省系统上传数据到部系统
       /// <summary>
       /// 待办箱中数据上传
       /// </summary>
       /// <param name="LogPath">日志保存根目录</param>
        public void UpToDoBox(string LogPath)
        {
            string[] arr = { "1","2","3","4"};
            for (int i = 0; i < arr.Length; i++)
            {
                string txtResult = string.Empty;
                DataTable dt = UpToDoBoxSearch(arr[i]);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                    #region 计划上报阶段
                        if (i == 0) //上报计划阶段
                        {
                            txtResult=GenerateXml_JH(dt.Rows[j][0].ToString());
                            if (txtResult.Equals("成功"))
                            {
                                string context=string.Format("{0}:sucucess  iid:{1}计划阶段数据上传成功！",DateTime.Now.ToString(),dt.Rows[j][0].ToString());
                                UpLog.Log_Save(LogPath,context);
                            }
                            else
                            {
                                //上传失败项目数据设置状态为：3
                                FailToUp(dt.Rows[j][0].ToString(),"1");
                                string context=string.Format("{0}:error  iid:{1}计划阶段数据上传失败！原因:{2}",DateTime.Now.ToString(),dt.Rows[j][0].ToString(),txtResult);
                                UpLog.Log_Save(LogPath,context);
                            }
                        }
                        #endregion
                    #region 实施上报阶段
                        else if (i== 1)//上报实施阶段
                        {
                            txtResult = GenerateXml_SS(dt.Rows[j][0].ToString());
                            if (txtResult.Equals("成功"))
                            {
                                string context = string.Format("{0}:sucucess  iid:{1}实施阶段数据上传成功！", DateTime.Now.ToString(), dt.Rows[j][0].ToString());
                                UpLog.Log_Save(LogPath, context);
                            }
                            else
                            {
                                //上传失败项目数据设置状态为：3
                                FailToUp(dt.Rows[j][0].ToString(), "2");
                                string context = string.Format("{0}:error  iid:{1}实施阶段数据上传失败！原因:{2}", DateTime.Now.ToString(), dt.Rows[j][0].ToString(),txtResult);
                                UpLog.Log_Save(LogPath, context);
                            }
                        }
                    #endregion
                    #region 验收上报阶段
                        else if (i == 2)//上报验收阶段
                        {
                            txtResult=GenerateXml_YS(dt.Rows[j][0].ToString());
                            if (txtResult.Equals("成功"))
                            {
                                string context = string.Format("{0}:sucucess  iid:{1}验收阶段数据上传成功！", DateTime.Now.ToString(), dt.Rows[j][0].ToString());
                                UpLog.Log_Save(LogPath, context);
                            }
                            else
                            {
                                //上传失败项目数据设置状态为：3
                                FailToUp(dt.Rows[j][0].ToString(), "3");
                                string context = string.Format("{0}:error  iid:{1}验收阶段数据上传失败！原因:{2}", DateTime.Now.ToString(), dt.Rows[j][0].ToString(),txtResult);
                                UpLog.Log_Save(LogPath, context);
                            }
                        }
                    #endregion
                    #region 高标地块上报
                        else if (i == 3)//上报高标地块
                        {
                            txtResult=GetGBZ(dt.Rows[j][0].ToString());
                            if(txtResult.Equals("成功"))
                            {
                                string context = string.Format("{0}:sucucess  iid:{1}高标准农田数据上传成功！", DateTime.Now.ToString(), dt.Rows[j][0].ToString());
                                UpLog.Log_Save(LogPath, context);
                            }
                            else
                            {
                                //上传失败项目数据设置状态为：3
                                FailToUp(dt.Rows[j][0].ToString(), "4");
                                string context = string.Format("{0}:error  iid:{1}高标准农田数据上传失败！原因：{2}", DateTime.Now.ToString(), dt.Rows[j][0].ToString(),txtResult);
                                UpLog.Log_Save(LogPath, context);
                            }
                        }
                    #endregion
                        
                    }            
                      
                }
            }
        }
        /// <summary>
        /// 查询得到待办箱数据
        /// </summary>
        /// <returns></returns>
        public DataTable UpToDoBoxSearch(string type)
        {
            string strSql = "select iid from UT_S2BSYN_RECORD where {0}=2 ";
            //查询到待办箱中的数据的IID
            string colname = "";
            switch (type)
            {
                case "1":
                    colname = "JHUPLOAD";
                    break;
                case "2":
                    colname = "SSUPLOAD";
                    break;
                case "3":
                    colname = "YSUPLOAD";
                    break;
                case "4":
                    colname = "GBUPLOAD";
                    break;
            }
            strSql = string.Format(strSql, colname);

            DataTable dt = new DataTable();
            dt = db_tdzzyw.Query4DataTable(strSql,"");
            return dt;
        }


        /// <summary>
        /// 示范已经上传
        /// </summary>
        /// <param name="iid">业务编号</param>
        /// <param name="type">同步类型 1：计划  2：实施 3：验收</param>
        /// <returns></returns>
        private bool IsUpload(string iid, string type)
        {
            string strSql = "select count(1) from UT_S2BSYN_RECORD where {0}=1 and iid='" + iid + "'";
            string colname = "";
            switch (type)
            {
                case "1":
                    colname = "JHUPLOAD";
                    break;
                case "2":
                    colname = "SSUPLOAD";
                    break;
                case "3":
                    colname = "YSUPLOAD";
                    break;
                case "4":
                    colname = "GBUPLOAD";
                    break;
            }
            strSql = string.Format(strSql, colname);

            int i = db_tdzzyw.Single<int>(strSql);

            return i > 0;
        }

        /// <summary>
        /// 导入计划阶段的XML到部系统
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public string GenerateXml_JH(string iid)
        {
            if (IsUpload(iid, "1"))
            {
                return "成功";
            }
                

            string[] strArray = iid.Split('|');
            iid = strArray[0];
            string result = string.Empty;
            db_tdzzyw.BeginTransaction();
            try
            {
                //根据iid查询到计划表的相关信息
                string sql = string.Empty;
                //iid = "201611100001414";
                //根据iid查询计划表的数据
                if (!string.IsNullOrEmpty(iid))
                {
                    sql += string.Format(" select * from vi_plan u where u.iid={0}", iid);
                }
                DataTable dt = new DataTable();
                dt = db_tdzzyw.Query4DataTable(sql, "");
                if (dt.Rows.Count > 0)
                {
                    StringBuilder xml = new StringBuilder(File.ReadAllText(jhxmlpath));
                    DataRow dr = dt.Rows[0];
                    foreach (DataColumn col in dt.Columns)
                    {
                        string MatchChar = "{" + col.ColumnName.ToUpper() + "}";
                        string MatchValue = dr[col.ColumnName].ToString();
                        xml.Replace(MatchChar, MatchValue);
                    }

                    //地块坐标
                    #region 获取地块坐标内容
                    string b_sql = string.Format("select distinct b.*,p.jd as xmjd from ut_sp_survey_bound b,UT_SP_SURVEY_PLOT p where b.id_survey_bound=p.id_survey_bound and b.sx_iid={0} and p.jd in(0,2) ", iid);
                    StringBuilder xmlzb = new StringBuilder();
                    DataTable dt_b = new DataTable();
                    dt_b = db_tdzzyw.Query4DataTable(b_sql, "");
                    foreach (DataRow dr_b in dt_b.Rows)
                    {
                        xmlzb.Append("<Z_JH_TAC_DTO>\r\n");
                        xmlzb.AppendFormat("<COM_UNIT>{0}</COM_UNIT>\r\n", getStr(dr_b["JLDW"]));//计量单位 90
                        xmlzb.AppendFormat("<TAC_PRECISION>{0}</TAC_PRECISION>\r\n", dr_b["JD"]);//精度
                        xmlzb.AppendFormat("<PROJECTION_TYPE>{0}</PROJECTION_TYPE>\r\n", dr_b["TYLX"]);//投影类型
                        xmlzb.AppendFormat("<HOW_MANY_ANGLE>{0}</HOW_MANY_ANGLE>\r\n", dr_b["JDFD"]);//几分几带
                        xmlzb.AppendFormat("<TERRAIN_NO>{0}</TERRAIN_NO>\r\n", dr_b["DH"]);//带号
                        xmlzb.AppendFormat("<X_MOVE>{0}</X_MOVE>\r\n", 0);//X平移
                        xmlzb.AppendFormat("<Y_MOVE>{0}</Y_MOVE>\r\n", 0);//Y平移
                        xmlzb.AppendFormat("<Z_MOVE>{0}</Z_MOVE>\r\n", 0);//Z平移
                        xmlzb.AppendFormat("<X_ROTATE>{0}</X_ROTATE>\r\n", 0);//X旋转
                        xmlzb.AppendFormat("<Y_ROTATE>{0}</Y_ROTATE>\r\n", 0);//Y旋转
                        xmlzb.AppendFormat("<Z_ROTATE>{0}</Z_ROTATE>\r\n", 0);//Z旋转
                        xmlzb.AppendFormat("<MEASURE>{0}</MEASURE>\r\n", 0);//尺度
                        xmlzb.AppendFormat("<COORD_SYS>{0}</COORD_SYS>\r\n", dr_b["ZBX"]);//坐标系

                        xmlzb.AppendFormat("<SFGBZ>{0}</SFGBZ>\r\n", dr_b["xmjd"].ToString() == "2" ? "H" : "N");//是否高标准基本农田
                        xmlzb.AppendFormat("<JSD>{0}</JSD>\r\n", "");//建设地点


                        xmlzb.Append("<Z_JH_PLOT_DTOs>\r\n");

                        string sql_dikuai = string.Format("select * from UT_SP_SURVEY_PLOT p where p.sx_iid={0} and p.id_survey_bound='{1}' ", iid, dr_b["id_survey_bound"].ToString());
                        DataTable dt_dikuai = new DataTable();
                        dt_dikuai = db_tdzzyw.Query4DataTable(sql_dikuai, "");
                        if (dt_dikuai.Rows.Count > 0)
                        {
                            //存在多个地块的情况
                            for (int i = 0; i < dt_dikuai.Rows.Count; i++)
                            {
                                DataRow dr_dikuai = dt_dikuai.Rows[i];
                                xmlzb.Append("<Z_JH_PLOT_DTO>\r\n");
                                xmlzb.AppendFormat("<PL_NO>{0}</PL_NO>\r\n", dr_dikuai["ID_PLOT_SORT"]);//地块号
                                xmlzb.AppendFormat("<PL_NAME>{0}</PL_NAME>\r\n", dr_dikuai["NA_PLOT"]);//地块名称
                                xmlzb.AppendFormat("<MAP_NO>{0}</MAP_NO>\r\n", dr_dikuai["ID_MAP"]);//图幅号
                                xmlzb.AppendFormat("<PNT_COUNT>{0}</PNT_COUNT>\r\n", dr_dikuai["QT_BOUND"]);//坐标点数
                                xmlzb.AppendFormat("<SHAPE_TYPE>{0}</SHAPE_TYPE>\r\n", dr_dikuai["TX_SHAPE_TYPE"]);//记录图形属性
                                xmlzb.AppendFormat("<PURPOSE>{0}</PURPOSE>\r\n", dr_dikuai["TX_PLOT_USE"]);//地块用途
                                xmlzb.AppendFormat("<DIKUAI_NO>{0}</DIKUAI_NO>\r\n", dr_dikuai["ID_PLOT"]);//地块编号
                                xmlzb.AppendFormat("<DIKUAI_AREA>{0}</DIKUAI_AREA>\r\n", getDouble4(dr_dikuai["QT_PLOT_AREA"]));//地块面积
                                xmlzb.AppendFormat("<PNT_SURVEY>{0}</PNT_SURVEY>\r\n", dr_dikuai["NA_SURVEY"]);//测量人
                                xmlzb.AppendFormat("<PNT_CHECKER>{0}</PNT_CHECKER>\r\n", dr_dikuai["NA_AUDITING"]);//复核人
                                xmlzb.AppendFormat("<SURVEY_DATE>{0}</SURVEY_DATE>\r\n", getDate(dr_dikuai["DT_SURVEY"]));//日期
                                xmlzb.Append("<Z_JH_PNT_COORD_DTOs>\r\n");
                                DataTable dt_p1 = new DataTable();
                                //查询该地块的坐标
                                string Psql1 = string.Format("select * from UT_SP_SURVEY_PLOT_PNT p where p.id_survey_plot='{0}' and p.sx_iid={1} order by id_survey_plot,id_sort", getStr(dr_dikuai["id"]), iid);
                                dt_p1 = db_tdzzyw.Query4DataTable(Psql1, "");
                                if (dt_p1.Rows.Count > 0)
                                {
                                    //遍历该项目该地块的所有坐标
                                    for (int j = 0; j < dt_p1.Rows.Count; j++)
                                    {
                                        DataRow dr_zuobiao = dt_p1.Rows[j];
                                        xmlzb.Append("<Z_JH_PNT_COORD_DTO>\r\n");
                                        xmlzb.AppendFormat("<PNT_NO>{0}</PNT_NO>\r\n", getStr(dr_zuobiao["ID_BOUND"]));//序号
                                        xmlzb.AppendFormat("<X_COORD>{0}</X_COORD>\r\n", dr_zuobiao["QT_Y_COORDINATE"]);//X轴
                                        xmlzb.AppendFormat("<Y_COORD>{0}</Y_COORD>\r\n", dr_zuobiao["QT_X_COORDINATE"]);//Y轴
                                        xmlzb.AppendFormat("<BORDER_LENGTH>{0}</BORDER_LENGTH>\r\n", getDouble2(dr_zuobiao["QT_BORDER_LENGTH"]));//反算边长
                                        xmlzb.AppendFormat("<MATERIAL>{0}</MATERIAL>\r\n", "");//坐标加密串
                                        xmlzb.AppendFormat("<REMARK>{0}</REMARK>\r\n", dr_zuobiao["DS_REMARK"]);//备注
                                        xmlzb.AppendFormat("<SHAPE_GROUP>{0}</SHAPE_GROUP>\r\n", dr_zuobiao["QT_PLOT_RING"]);//地块圈号
                                        xmlzb.AppendFormat("<PNT_SERIAL>{0}</PNT_SERIAL>\r\n", j + 1);//拐点号
                                        xmlzb.Append("</Z_JH_PNT_COORD_DTO>\r\n");
                                    }
                                }
                                xmlzb.Append("</Z_JH_PNT_COORD_DTOs>\r\n");
                                xmlzb.Append("</Z_JH_PLOT_DTO>\r\n");
                            }
                        }
                        else
                        {
                            xmlzb.Append("<Z_JH_PLOT_DTO>\r\n");
                            xmlzb.AppendFormat("<PL_NO>{0}</PL_NO>\r\n", 0);//地块号
                            xmlzb.AppendFormat("<PL_NAME>{0}</PL_NAME>\r\n", "");//地块名称
                            xmlzb.AppendFormat("<MAP_NO>{0}</MAP_NO>\r\n", "");//图幅号
                            xmlzb.AppendFormat("<PNT_COUNT>{0}</PNT_COUNT>\r\n", 0);//坐标点数
                            xmlzb.AppendFormat("<SHAPE_TYPE>{0}</SHAPE_TYPE>\r\n", "");//记录图形属性
                            xmlzb.AppendFormat("<PURPOSE>{0}</PURPOSE>\r\n", "");//地块用途
                            xmlzb.AppendFormat("<DIKUAI_NO>{0}</DIKUAI_NO>\r\n", "");//地块编号
                            xmlzb.AppendFormat("<DIKUAI_AREA>{0}</DIKUAI_AREA>\r\n", 0);//地块面积
                            xmlzb.AppendFormat("<PNT_SURVEY>{0}</PNT_SURVEY>\r\n", "");//测量人
                            xmlzb.AppendFormat("<PNT_CHECKER>{0}</PNT_CHECKER>\r\n", "");//复核人
                            xmlzb.AppendFormat("<SURVEY_DATE>{0}</SURVEY_DATE>\r\n", "");//日期
                            xmlzb.Append("<Z_JH_PNT_COORD_DTOs>\r\n");
                            xmlzb.Append("<Z_JH_PNT_COORD_DTO>\r\n");
                            xmlzb.AppendFormat("<PNT_NO>{0}</PNT_NO>\r\n", "");//序号
                            xmlzb.AppendFormat("<X_COORD>{0}</X_COORD>\r\n", 0);//X轴
                            xmlzb.AppendFormat("<Y_COORD>{0}</Y_COORD>\r\n", 0);//Y轴
                            xmlzb.AppendFormat("<BORDER_LENGTH>{0}</BORDER_LENGTH>\r\n", 0);//反算边长
                            xmlzb.AppendFormat("<MATERIAL>{0}</MATERIAL>\r\n", "");//坐标加密串
                            xmlzb.AppendFormat("<REMARK>{0}</REMARK>\r\n", "");//备注
                            xmlzb.AppendFormat("<SHAPE_GROUP>{0}</SHAPE_GROUP>\r\n", 0);//地块圈号
                            xmlzb.AppendFormat("<PNT_SERIAL>{0}</PNT_SERIAL>\r\n", 0);//拐点号
                            xmlzb.Append("</Z_JH_PNT_COORD_DTO>\r\n");
                            xmlzb.Append("</Z_JH_PNT_COORD_DTOs>\r\n");
                            xmlzb.Append("</Z_JH_PLOT_DTO>\r\n");
                        }
                        xmlzb.Append("</Z_JH_PLOT_DTOs>\r\n");
                        xmlzb.Append("</Z_JH_TAC_DTO>\r\n");
                    }
                    #endregion

                    xml.Replace("{Z_JH_TAC_DTOs}", xmlzb.ToString());

                    string XmlStr = xml.ToString();
                    ExceptionLog.WriteLog(XmlStr);
                    cn.gov.mlr.tdzz.DataImportService client = new cn.gov.mlr.tdzz.DataImportService();
                    client.Timeout = 30 * 60 * 1000;
                    //接口为部系统的测试接口，账号和密码也为测试系统的账号和密码
                    result = client.ImportAndDoReport(bxt_username, bxt_password, bxt_xzqbm, XmlStr, "0");
                    if (result.Contains("成功"))//如果导入成功，将该记录的项目预算编号修改为原系统的预算编号
                    {
                        string[] resultArray = result.Split(':');
                        string xmysbh = resultArray[1];
                        //更新数据的项目预算编号
                        string strsql = string.Format("update ut_plan set ysbh='{0}' where iid={1}", xmysbh, iid);
                        db_tdzzyw.Execute(strsql);

                        WriteS2BSYN_RECORD(iid, "1", "1");
                    }
                }
                else
                {
                    result = "无法查询到可上传的计划数据,请核实项目实施数据是否完整！";
                }

                db_tdzzyw.CompleteTransaction();
            }
            catch (Exception ex)
            {
                db_tdzzyw.AbortTransaction();
                ExceptionLog.WriteExceptionLog(ex);
                result = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 导入实施阶段xml数据到部系统（实施阶段导入xml必须在部系统该项目计划阶段审核通过之后）
        /// </summary>
        /// <param name="iid"></param>
        public string GenerateXml_SS(string iid)
        {
            if (IsUpload(iid, "2")) return "成功";

            string[] strArray = iid.Split('|');
            iid = strArray[0];
            string result = string.Empty;
            db_tdzzyw.BeginTransaction();
            try
            {
                //根据iid查询到计划表的相关信息
                string sql = string.Empty;
                if (!string.IsNullOrEmpty(iid))
                {
                    sql += string.Format(" select * from vi_implement i where i.iid={0}", iid);
                }
                DataTable dt = new DataTable();
                dt = db_tdzzyw.Query4DataTable(sql, "");
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    if (string.IsNullOrEmpty(getStr(dr["JBXX_YSBH"])))
                    {
                        return "项目预算编号不能为空，导入失败！";
                    }
                    StringBuilder xml = new StringBuilder(File.ReadAllText(ssxmlpath));
                    foreach (DataColumn col in dt.Columns)
                    {
                        string MatchChar = "{" + col.ColumnName.ToUpper() + "}";
                        string MatchValue = dr[col.ColumnName].ToString();
                        xml.Replace(MatchChar, MatchValue);
                    }

                    #region 资金到位情况
                    StringBuilder xmldwqk = new StringBuilder();
                    string[] dqdwzjArray = getStr(dr["DQDWZJ"]).Split(',');
                    string[] dqzcjeArray = getStr(dr["DQZCJE"]).Split(',');
                    //存在多个当期到位资金和支出资金的情况
                    for (int n = 0; n < dqdwzjArray.Length; n++)
                    {
                        xmldwqk.Append("<Z_DWQK_DTO>\r\n");
                        xmldwqk.AppendFormat("<DWQK_DWSJ>{0}</DWQK_DWSJ>\r\n", "0001-01-01");//到位时间
                        xmldwqk.AppendFormat("<DWQK_TZ_DWZJ>{0}</DWQK_TZ_DWZJ>\r\n", getDouble4(dqdwzjArray[n]));//当期到位资金
                        xmldwqk.AppendFormat("<DWQK_TZ_ZYZCZJ>{0}</DWQK_TZ_ZYZCZJ>\r\n", 0);//当期到位中央支持资金
                        xmldwqk.AppendFormat("<DWQK_TZ_DFZJ>{0}</DWQK_TZ_DFZJ>\r\n", 0);//当期到位地方资金
                        xmldwqk.AppendFormat("<DWQK_TZ_ZYFPXZF>{0}</DWQK_TZ_ZYFPXZF>\r\n", 0);//当期到位中央分配的新增费资金
                        xmldwqk.AppendFormat("<DWQK_TZ_DFLCXZF>{0}</DWQK_TZ_DFLCXZF>\r\n", 0);//当期到位地方留成新增费资金
                        xmldwqk.AppendFormat("<DWQK_TZ_GDKKF>{0}</DWQK_TZ_GDKKF>\r\n", 0);//当期到位耕地开垦费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZXBCGDZJ>{0}</DWQK_TZ_ZXBCGDZJ>\r\n", 0);//当期到位自行补充耕地资金
                        xmldwqk.AppendFormat("<DWQK_TZ_CRJ>{0}</DWQK_TZ_CRJ>\r\n", 0);//当期到位用于农业开发的土地出让收入
                        xmldwqk.AppendFormat("<DWQK_TZ_FKF>{0}</DWQK_TZ_FKF>\r\n", 0);//当期到位土地开垦费
                        xmldwqk.AppendFormat("<DWQK_TZ_FKYWRTZ>{0}</DWQK_TZ_FKYWRTZ>\r\n", 0);//当期到位复垦义务人投资
                        xmldwqk.AppendFormat("<DWQK_TZ_QTZJ>{0}</DWQK_TZ_QTZJ>\r\n", 0);//当期到位其他资金
                        xmldwqk.AppendFormat("<DWQK_TZ_ZCJE>{0}</DWQK_TZ_ZCJE>\r\n", getDouble4(dqzcjeArray[n]));//当期支出金额
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_GCSGF>{0}</DWQK_TZ_ZC_GCSGF>\r\n", 0);//当期支出工程施工费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_SBGZF>{0}</DWQK_TZ_ZC_SBGZF>\r\n", 0);//当期支出设备购置费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QTFY>{0}</DWQK_TZ_ZC_QTFY>\r\n", 0);//当期支出其他费用
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QT_QQGZF>{0}</DWQK_TZ_ZC_QT_QQGZF>\r\n", 0);//前期工作费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QT_GCJLF>{0}</DWQK_TZ_ZC_QT_GCJLF>\r\n", 0);//工程监理费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QT_CQBCF>{0}</DWQK_TZ_ZC_QT_CQBCF>\r\n", 0);//拆迁补偿费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QT_JGYSF>{0}</DWQK_TZ_ZC_QT_JGYSF>\r\n", 0);//竣工验收费
                        xmldwqk.AppendFormat("<DWQK_TZ_ZC_QT_YZGLF>{0}</DWQK_TZ_ZC_QT_YZGLF>\r\n", 0);//业主管理费
                        xmldwqk.AppendFormat("<DWQK_RW_ZGM>{0}</DWQK_RW_ZGM>\r\n", 0);//当期完成建设规模
                        xmldwqk.AppendFormat("<DWQK_RW_GDMJ>{0}</DWQK_RW_GDMJ>\r\n", 0);//当期完成耕地面积
                        xmldwqk.AppendFormat("<DWQK_RW_JBNTZZMJ>{0}</DWQK_RW_JBNTZZMJ>\r\n", 0);//当期完成基本农田整治面积
                        xmldwqk.AppendFormat("<DWQK_RW_XZGDMJ>{0}</DWQK_RW_XZGDMJ>\r\n", 0);//当期完成新增耕地面积
                        xmldwqk.AppendFormat("<DWQK_RW_GBZ>{0}</DWQK_RW_GBZ>\r\n", 0);//当期完成高标准基本农田面积
                        xmldwqk.AppendFormat("<DWQK_GCL_TDPZMJ>{0}</DWQK_GCL_TDPZMJ>\r\n", 0);//当期土地平整面积
                        xmldwqk.AppendFormat("<DWQK_GCL_TFGCL>{0}</DWQK_GCL_TFGCL>\r\n", 0);//当期土方工程量
                        xmldwqk.AppendFormat("<DWQK_GCL_GD>{0}</DWQK_GCL_GD>\r\n", 0);//当期灌排管道
                        xmldwqk.AppendFormat("<DWQK_GCL_GD_GGQD>{0}</DWQK_GCL_GD_GGQD>\r\n", 0);//当期完成骨干渠道
                        xmldwqk.AppendFormat("<DWQK_GCL_GD_GGGD>{0}</DWQK_GCL_GD_GGGD>\r\n", 0);//当期完成骨干管道
                        xmldwqk.AppendFormat("<DWQK_GCL_TJDLGCL>{0}</DWQK_GCL_TJDLGCL>\r\n", 0);//当期田间道路工程量
                        xmldwqk.AppendFormat("<DWQK_GCL_TJDLGCL_TJD>{0}</DWQK_GCL_TJDLGCL_TJD>\r\n", 0);//当期完成田间道
                        xmldwqk.AppendFormat("<DWQK_GCL_TJDLGCL_SCL>{0}</DWQK_GCL_TJDLGCL_SCL>\r\n", 0);//当期完成生产路
                        xmldwqk.AppendFormat("<DWQK_GCL_NTFHLGCL>{0}</DWQK_GCL_NTFHLGCL>\r\n", 0);//当期农田防护林工程量
                        xmldwqk.AppendFormat("<DWQK_GCL_BZ>{0}</DWQK_GCL_BZ>\r\n", 0);//当期完成泵站
                        xmldwqk.AppendFormat("<DWQK_GCL_XSC>{0}</DWQK_GCL_XSC>\r\n", 0);//当期完成蓄水池
                        xmldwqk.AppendFormat("<DWQK_GCL_NYJ>{0}</DWQK_GCL_NYJ>\r\n", 0);//当期完成农用井
                        xmldwqk.AppendFormat("<DWQK_GCL_NYQ>{0}</DWQK_GCL_NYQ>\r\n", 0);//当期完成农用桥
                        xmldwqk.AppendFormat("<DWQK_GCL_GYSPDXL>{0}</DWQK_GCL_GYSPDXL>\r\n", 0);//当期完成高压输配电线路
                        xmldwqk.AppendFormat("<DWQK_GCL_DYSPDXL>{0}</DWQK_GCL_DYSPDXL>\r\n", 0);//当期完成低压输电线路
                        xmldwqk.AppendFormat("<DWQK_GCL_JMFQFK>{0}</DWQK_GCL_JMFQFK>\r\n", 0);//当期完成废弃居民点复垦面积
                        xmldwqk.AppendFormat("<DWQK_GCL_GKFQFK>{0}</DWQK_GCL_GKFQFK>\r\n", 0);//当期完成工矿废弃地复垦面积
                        xmldwqk.AppendFormat("<DWQK_ISSH>{0}</DWQK_ISSH>\r\n", "1");//是否通过审核
                        xmldwqk.Append("</Z_DWQK_DTO>\r\n");
                    }
                    #endregion
                    xml.Replace("{Z_DWQK_DTOs}", xmldwqk.ToString());


                    #region 规划单位
                    StringBuilder xmlxgzjgh = new StringBuilder();
                    string[] ghsjdwmc = getStr(dr["GHSJDWMC"]).Split(',');
                    string[] ghsjdwdm = getStr(dr["GHSJDWDM"]).Split(',');
                    for (int i = 0; i < ghsjdwmc.Length; i++)
                    {
                        xmlxgzjgh.Append("<Z_XGZJ_GH_DTO>\r\n");
                        xmlxgzjgh.AppendFormat("<GHSJDW_MC>{0}</GHSJDW_MC>\r\n", ghsjdwmc[i]);//规划单位名称
                        xmlxgzjgh.AppendFormat("<GHSJDW_DM>{0}</GHSJDW_DM>\r\n", ghsjdwdm[i]);//规划单位代码
                        xmlxgzjgh.Append("</Z_XGZJ_GH_DTO>\r\n");
                    }
                    #endregion
                    xml.Replace("{Z_XGZJ_GH_DTOs}", xmlxgzjgh.ToString());

                    #region 监理单位
                    StringBuilder xmlxgzjjl = new StringBuilder();
                    string[] jldwmc = getStr(dr["JLDWMC"]).Split(',');
                    string[] jldwdm = getStr(dr["JLDWDM"]).Split(',');
                    for (int m = 0; m < jldwmc.Length; m++)
                    {
                        xmlxgzjjl.Append("<Z_XGZJ_JL_DTO>\r\n");
                        xmlxgzjjl.AppendFormat("<JLDW_MC>{0}</JLDW_MC>\r\n", getStr(jldwmc[m]));//监理单位名称
                        xmlxgzjjl.AppendFormat("<JLDW_DM>{0}</JLDW_DM>\r\n", getStr(jldwdm[m]));//监理单位代码
                        xmlxgzjjl.Append("</Z_XGZJ_JL_DTO>\r\n");
                    }
                    #endregion
                    xml.Replace("{Z_XGZJ_JL_DTOs}", xmlxgzjjl.ToString());

                    #region 施工单位
                    StringBuilder xmlxgzjsg = new StringBuilder();
                    string[] sgdwmc = getStr(dr["SGDWMC"]).Split(',');
                    string[] sgdwdm = getStr(dr["SGDWDM"]).Split(',');
                    for (int j = 0; j < sgdwmc.Length; j++)
                    {
                        xmlxgzjsg.Append("<Z_XGZJ_SG_DTO>\r\n");
                        xmlxgzjsg.AppendFormat("<SGDW_MC>{0}</SGDW_MC>\r\n", getStr(sgdwmc[j]));//施工单位名称
                        xmlxgzjsg.AppendFormat("<SGDW_DM>{0}</SGDW_DM>\r\n", getStr(sgdwdm[j]));//施工单位代码
                        xmlxgzjsg.Append("</Z_XGZJ_SG_DTO>\r\n");
                    }
                    #endregion
                    xml.Replace("{Z_XGZJ_SG_DTOs}", xmlxgzjsg.ToString());

                    #region 招标单位
                    StringBuilder xmlxgzjzb = new StringBuilder();
                    string[] zbdwmc = getStr(dr["ZBDLDWMC"]).Split(',');
                    string[] zbdwdm = getStr(dr["ZBDLDWDM"]).Split(',');
                    for (int n = 0; n < zbdwmc.Length; n++)
                    {
                        xmlxgzjzb.Append("<Z_XGZJ_ZB_DTO>\r\n");
                        xmlxgzjzb.AppendFormat("<ZBDLDW_MC>{0}</ZBDLDW_MC>\r\n", getStr(zbdwmc[n]));//招标单位名称
                        xmlxgzjzb.AppendFormat("<ZBDLDW_DM>{0}</ZBDLDW_DM>\r\n", getStr(zbdwdm[n]));//招标单位代码
                        xmlxgzjzb.Append("</Z_XGZJ_ZB_DTO>\r\n");
                    }
                    #endregion
                    xml.Replace("{Z_XGZJ_ZB_DTOs}", xmlxgzjzb.ToString());


                    string XmlStr = xml.ToString();
                    ExceptionLog.WriteLog(XmlStr);
                    cn.gov.mlr.tdzz.DataImportService client = new cn.gov.mlr.tdzz.DataImportService();
                    result = client.ImportAndDoReport(bxt_username, bxt_password, bxt_xzqbm, XmlStr, "1");//0计划 1实施 2验收 
                    if (result.Contains("该操作不符合规范，无法进行"))
                    {
                        result = "部系统计划阶段审核尚未通过或者项目预算编号错误！";
                    }
                    else if (result.Contains("成功"))
                    {
                        WriteS2BSYN_RECORD(iid, "2", "1");
                    }
                }
                else
                {
                    result = "无法查询到可上传的实施数据,请核实项目实施数据是否完整！";
                }

                db_tdzzyw.CompleteTransaction();
            }
            catch (Exception ex)
            {
                db_tdzzyw.AbortTransaction();
                ExceptionLog.WriteExceptionLog(ex);
                result = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 导入验收阶段的XML到部系统（验收阶段的导入必须在部系统该项目实施阶段审核通过之后）
        /// </summary>
        /// <param name="iid"></param>
        public string GenerateXml_YS(string iid)
        {
            if (IsUpload(iid, "3")) return "成功";

            string[] strArray = iid.Split('|');
            iid = strArray[0];
            string result = string.Empty;
            //iid = "201611100000971";
            db_tdzzyw.BeginTransaction();
            try
            {
                string sql = string.Empty;
                if (!string.IsNullOrEmpty(iid))
                {
                    sql += string.Format(@"select * from vi_ACCEPTANCE a where a.iid={0}", iid);
                    DataTable dt = new DataTable();
                    dt = db_tdzzyw.Query4DataTable(sql, "");
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        if (string.IsNullOrEmpty(getStr(dr["JBXX_YSBH"])))
                        {
                            return "项目预算编号不能为空，导入失败！";
                        }
                        StringBuilder xml = new StringBuilder(File.ReadAllText(ysxmlpath));
                        foreach (DataColumn col in dt.Columns)
                        {
                            string MatchChar = "{" + col.ColumnName.ToUpper() + "}";
                            string MatchValue = dr[col.ColumnName].ToString();
                            xml.Replace(MatchChar, MatchValue);
                        }

                        #region 地块信息
                        StringBuilder xmlzb = new StringBuilder();
                        string b_sql = string.Format("select distinct b.*,p.jd as xmjd from ut_sp_survey_bound b,UT_SP_SURVEY_PLOT p where b.id_survey_bound=p.id_survey_bound and b.sx_iid={0} and p.jd in(1,3) ", iid);
                        DataTable dt_b = new DataTable();
                        dt_b = db_tdzzyw.Query4DataTable(b_sql, "");
                        foreach (DataRow dr_b in dt_b.Rows)
                        {
                            xmlzb.Append("<Z_TAC_DTO>\r\n");
                            xmlzb.AppendFormat("<COM_UNIT>{0}</COM_UNIT>\r\n", getStr(dr_b["JLDW"]));//计量单位 90
                            xmlzb.AppendFormat("<TAC_PRECISION>{0}</TAC_PRECISION>\r\n", dr_b["JD"]);//精度
                            xmlzb.AppendFormat("<PROJECTION_TYPE>{0}</PROJECTION_TYPE>\r\n", dr_b["TYLX"]);//投影类型
                            xmlzb.AppendFormat("<HOW_MANY_ANGLE>{0}</HOW_MANY_ANGLE>\r\n", dr_b["JDFD"]);//几分几带
                            xmlzb.AppendFormat("<TERRAIN_NO>{0}</TERRAIN_NO>\r\n", dr_b["DH"]);//带号
                            xmlzb.AppendFormat("<X_MOVE>{0}</X_MOVE>\r\n", 0);//X平移
                            xmlzb.AppendFormat("<Y_MOVE>{0}</Y_MOVE>\r\n", 0);//Y平移
                            xmlzb.AppendFormat("<Z_MOVE>{0}</Z_MOVE>\r\n", 0);//Z平移
                            xmlzb.AppendFormat("<X_ROTATE>{0}</X_ROTATE>\r\n", 0);//X旋转
                            xmlzb.AppendFormat("<Y_ROTATE>{0}</Y_ROTATE>\r\n", 0);//Y旋转
                            xmlzb.AppendFormat("<Z_ROTATE>{0}</Z_ROTATE>\r\n", 0);//Z旋转
                            xmlzb.AppendFormat("<MEASURE>{0}</MEASURE>\r\n", 0);//尺度
                            xmlzb.AppendFormat("<COORD_SYS>{0}</COORD_SYS>\r\n", dr_b["ZBX"]);//坐标系
                            xmlzb.AppendFormat("<SFGBZ>{0}</SFGBZ>\r\n", dr_b["xmjd"].ToString() == "3" ? "H" : "N");//是否高标准基本农田
                            xmlzb.AppendFormat("<JSD>{0}</JSD>\r\n", "");//建设地点

                            xmlzb.Append("<Z_PLOT_DTOs>\r\n");
                            string sql_dikuai = string.Format("select * from UT_SP_SURVEY_PLOT p where p.sx_iid={0} and p.id_survey_bound='{1}'", iid, dr_b["id_survey_bound"].ToString());
                            DataTable dt_dikuai = new DataTable();
                            dt_dikuai = db_tdzzyw.Query4DataTable(sql_dikuai, "");
                            if (dt_dikuai.Rows.Count > 0)
                            {
                                for (int i = 0; i < dt_dikuai.Rows.Count; i++)
                                {
                                    DataRow dr_dikuai = dt_dikuai.Rows[i];
                                    xmlzb.Append("<Z_PLOT_DTO>\r\n");
                                    xmlzb.AppendFormat("<PL_NO>{0}</PL_NO>\r\n", dr_dikuai["ID_PLOT_SORT"]);//地块号
                                    xmlzb.AppendFormat("<PL_NAME>{0}</PL_NAME>\r\n", dr_dikuai["NA_PLOT"]);//地块名称
                                    xmlzb.AppendFormat("<MAP_NO>{0}</MAP_NO>\r\n", dr_dikuai["ID_MAP"]);//图幅号
                                    xmlzb.AppendFormat("<PNT_COUNT>{0}</PNT_COUNT>\r\n", dr_dikuai["QT_BOUND"]);//坐标点数
                                    xmlzb.AppendFormat("<SHAPE_TYPE>{0}</SHAPE_TYPE>\r\n", dr_dikuai["TX_SHAPE_TYPE"]);//记录图形属性
                                    xmlzb.AppendFormat("<PURPOSE>{0}</PURPOSE>\r\n", dr_dikuai["TX_PLOT_USE"]);//地块用途
                                    xmlzb.AppendFormat("<DIKUAI_NO>{0}</DIKUAI_NO>\r\n", dr_dikuai["ID_PLOT"]);//地块编号
                                    xmlzb.AppendFormat("<DIKUAI_AREA>{0}</DIKUAI_AREA>\r\n", getDouble4(dr_dikuai["QT_PLOT_AREA"]));//地块面积
                                    xmlzb.AppendFormat("<PNT_SURVEY>{0}</PNT_SURVEY>\r\n", dr_dikuai["NA_SURVEY"]);//测量人
                                    xmlzb.AppendFormat("<PNT_CHECKER>{0}</PNT_CHECKER>\r\n", dr_dikuai["NA_AUDITING"]);//复核人
                                    xmlzb.AppendFormat("<SURVEY_DATE>{0}</SURVEY_DATE>\r\n", getDate(dr_dikuai["DT_SURVEY"]));//日期
                                    xmlzb.Append("<Z_PNT_COORD_DTOs>\r\n");
                                    DataTable dt_p1 = new DataTable();
                                    string Psql1 = string.Format("select * from UT_SP_SURVEY_PLOT_PNT p where p.id_survey_plot='{0}' and p.sx_iid={1} order by id_survey_plot,id_sort", getStr(dr_dikuai["id"]), iid);
                                    dt_p1 = db_tdzzyw.Query4DataTable(Psql1, "");
                                    if (dt_p1.Rows.Count > 0)
                                    {
                                        for (int j = 0; j < dt_p1.Rows.Count; j++)
                                        {
                                            DataRow dr_zuobiao = dt_p1.Rows[j];
                                            xmlzb.Append("<Z_PNT_COORD_DTO>\r\n");
                                            xmlzb.AppendFormat("<PNT_NO>{0}</PNT_NO>\r\n", getStr(dr_zuobiao["ID_BOUND"]));//序号
                                            xmlzb.AppendFormat("<X_COORD>{0}</X_COORD>\r\n", dr_zuobiao["QT_Y_COORDINATE"]);//X轴
                                            xmlzb.AppendFormat("<Y_COORD>{0}</Y_COORD>\r\n", dr_zuobiao["QT_X_COORDINATE"]);//Y轴
                                            xmlzb.AppendFormat("<BORDER_LENGTH>{0}</BORDER_LENGTH>\r\n", getDouble2(dr_zuobiao["QT_BORDER_LENGTH"]));//反算边长
                                            xmlzb.AppendFormat("<MATERIAL>{0}</MATERIAL>\r\n", "");//坐标加密串（坐标未加密）
                                            xmlzb.AppendFormat("<REMARK>{0}</REMARK>\r\n", dr_zuobiao["DS_REMARK"]);//备注
                                            xmlzb.AppendFormat("<SHAPE_GROUP>{0}</SHAPE_GROUP>\r\n", dr_zuobiao["QT_PLOT_RING"]);//地块圈号
                                            xmlzb.AppendFormat("<PNT_SERIAL>{0}</PNT_SERIAL>\r\n", j + 1);//拐点号
                                            xmlzb.Append("</Z_PNT_COORD_DTO>\r\n");
                                        }
                                    }
                                    xmlzb.Append("</Z_PNT_COORD_DTOs>\r\n");
                                    xmlzb.Append("</Z_PLOT_DTO>");
                                }
                            }
                            else
                            {
                                xmlzb.Append("<Z_PLOT_DTO>\r\n");
                                xmlzb.AppendFormat("<PL_NO>{0}</PL_NO>\r\n", "");//地块号
                                xmlzb.AppendFormat("<PL_NAME>{0}</PL_NAME>\r\n", "");//地块名称
                                xmlzb.AppendFormat("<MAP_NO>{0}</MAP_NO>\r\n", "");//图幅号
                                xmlzb.AppendFormat("<PNT_COUNT>{0}</PNT_COUNT>\r\n", "");//坐标点数
                                xmlzb.AppendFormat("<SHAPE_TYPE>{0}</SHAPE_TYPE>\r\n", "");//记录图形属性
                                xmlzb.AppendFormat("<PURPOSE>{0}</PURPOSE>\r\n", "");//地块用途
                                xmlzb.AppendFormat("<DIKUAI_NO>{0}</DIKUAI_NO>\r\n", "");//地块编号
                                xmlzb.AppendFormat("<DIKUAI_AREA>{0}</DIKUAI_AREA>\r\n", 0);//地块面积
                                xmlzb.AppendFormat("<PNT_SURVEY>{0}</PNT_SURVEY>\r\n", "");//测量人
                                xmlzb.AppendFormat("<PNT_CHECKER>{0}</PNT_CHECKER>\r\n", "");//复核人
                                xmlzb.AppendFormat("<SURVEY_DATE>{0}</SURVEY_DATE>\r\n", "");//日期
                                xmlzb.Append("<Z_PNT_COORD_DTOs>\r\n");
                                xmlzb.Append("<Z_PNT_COORD_DTO>\r\n");
                                xmlzb.AppendFormat("<PNT_NO>{0}</PNT_NO>\r\n", "");//序号
                                xmlzb.AppendFormat("<X_COORD>{0}</X_COORD>\r\n", 0);//X轴
                                xmlzb.AppendFormat("<Y_COORD>{0}</Y_COORD>\r\n", 0);//Y轴
                                xmlzb.AppendFormat("<BORDER_LENGTH>{0}</BORDER_LENGTH>\r\n", 0);//反算边长
                                xmlzb.AppendFormat("<MATERIAL>{0}</MATERIAL>\r\n", "");//坐标加密串
                                xmlzb.AppendFormat("<REMARK>{0}</REMARK>\r\n", "");//备注
                                xmlzb.AppendFormat("<SHAPE_GROUP>{0}</SHAPE_GROUP>\r\n", "");//地块圈号
                                xmlzb.AppendFormat("<PNT_SERIAL>{0}</PNT_SERIAL>\r\n", 0);//拐点号
                                xmlzb.AppendFormat("<PNT_SERIAL>{0}</PNT_SERTAL>\r\n", 0);
                                xmlzb.Append("</Z_PNT_COORD_DTO>\r\n");
                                xmlzb.Append("</Z_PNT_COORD_DTOs>\r\n");
                                xmlzb.Append("</Z_PLOT_DTO>\r\n");
                            }
                            xmlzb.Append("</Z_PLOT_DTOs>\r\n");
                            xmlzb.Append("</Z_TAC_DTO>\r\n");
                        }
                        #endregion
                        xml.Replace("{Z_TAC_DTOs}", xmlzb.ToString());

                        string XmlStr = xml.ToString();
                        ExceptionLog.WriteLog(XmlStr);
                        cn.gov.mlr.tdzz.DataImportService client = new cn.gov.mlr.tdzz.DataImportService();
                        //接口为部系统的测试接口，账号和密码也为测试系统的账号和密码
                        result = client.ImportAndDoReport(bxt_username, bxt_password, bxt_xzqbm, XmlStr, "2");//0计划 1实施 2验收
                        if (result.Contains("该操作不符合规范，无法进行"))
                        {
                            result = "部系统该项目实施阶段尚未通过审核或者项目预算编号不正确";
                        }
                        else if (result.Contains("成功"))
                        {
                            string[] resultArray = result.Split(':');
                            string xmysbh = resultArray[1];
                            //更新项目验收编号
                            string strsql = string.Format("update UT_ACCEPTANCE set XMYSBH='{0}' where iid={1}", xmysbh, iid);
                            db_tdzzyw.Execute(strsql);

                            WriteS2BSYN_RECORD(iid, "3", "1");
                        }
                    }
                    else
                    {
                        result = "无法查询到可上传的验收数据,请核实项目验收数据是否完整！";
                    }
                }

                db_tdzzyw.CompleteTransaction();
            }
            catch (Exception ex)
            {
                db_tdzzyw.AbortTransaction();
                ExceptionLog.WriteExceptionLog(ex);
                result = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 导入其他高标准农田信息到部系统
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public string GetGBZ(string iid)
        {
            if (IsUpload(iid, "4")) return "成功";

            string result = "";
            db_tdzzyw.BeginTransaction();
            try
            {
                string sql = "";
                if (!string.IsNullOrEmpty(iid))
                {
                    sql += string.Format("select * from VI_QTGBZNT where QTXM_GUID='{0}'", iid);
                }
                DataTable dt = new DataTable();
                dt = db_tdzzyw.Query4DataTable(sql, "");
                if (dt.Rows.Count > 0)
                {
                    StringBuilder xml = new StringBuilder(File.ReadAllText(qtgbzpath));
                    DataRow dr = dt.Rows[0];
                    xml.Replace("{QTXM_XMMC}", dr["QTXM_XMMC"].ToString());
                    xml.Replace("{QTXM_YSDW}", dr["QTXM_YSDW"].ToString());
                    xml.Replace("{QTXM_YSRQ}", dr["QTXM_YSRQ"].ToString());
                    xml.Replace("{QTXM_JSFS}", dr["QTXM_JSFS"].ToString());
                    xml.Replace("{QTXM_JSDD}", dr["QTXM_JSDD"].ToString());
                    xml.Replace("{QTXM_XZQDM}", dr["QTXM_XZQDM"].ToString());
                    xml.Replace("{QTXM_JSGM}", dr["QTXM_JSGM"].ToString());
                    xml.Replace("{QTXM_GBZGM}", dr["QTXM_GBZGM"].ToString());
                    xml.Replace("{QTXM_ZTZ}", dr["QTXM_ZTZ"].ToString());
                    xml.Replace("{QTXM_GUID}", dr["QTXM_GUID"].ToString());
                    xml.Replace("{QTGBZND}", dr["QTGBZND"].ToString());
                    string XmlStr = xml.ToString();
                    cn.gov.mlr.tdzz.DataImportService client = new cn.gov.mlr.tdzz.DataImportService();
                    result = client.ImportDoReportQTXMProject(bxt_username, bxt_password, bxt_xzqbm, XmlStr);
                    if (result.Contains("成功"))
                    {
                        WriteS2BSYN_RECORD(iid, "4", "1");
                    }
                }
                else
                {
                    result = "无法查询到可上传的其他高标准农田数据，请核实数据是否完整";
                }
                db_tdzzyw.CompleteTransaction();
            }
            catch (Exception ex)
            {
                db_tdzzyw.AbortTransaction();
                ExceptionLog.WriteExceptionLog(ex);
                result = ex.Message;
            }
            return result;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 返回字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string getStr(object obj)
        {
            if (obj != null)
            {
                return obj.ToString();
            }
            else
                return "";
        }

        /// <summary>
        /// 返回格式化后的时间
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string getDateTime(object obj)
        {
            DateTime da = DateTime.MinValue;

            if (obj != null)
            {
                try
                {
                    da = DateTime.Parse(obj.ToString());
                }
                catch
                {

                }
            }
            return da.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 返回整数
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int getInt(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToInt32(obj.ToString());
                }
                catch
                {
                    return 0;
                }
            }
            else
                return 0;
        }

        public static string getDouble2(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToDouble(obj.ToString()).ToString("0.00");
                }
                catch
                {
                    return "0";
                }
            }
            else
                return "0";
        }

        /// <summary>
        /// 返回double类型的数据（保留小数点后三位数）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getDouble3(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToDouble(obj.ToString()).ToString("0.000");
                }
                catch
                {
                    return "0";
                }
            }
            else
                return "0";
        }

        /// <summary>
        /// 返回double类型的数据（保留小数点收四位数）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getDouble4(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToDouble(obj.ToString()).ToString("0.0000");
                }
                catch
                {
                    return "0";
                }
            }
            else
                return "0";
        }

        /// <summary>
        /// 返回日期类型的数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getDate(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToDateTime(obj).ToString("yyyy-MM-dd");
                }
                catch
                {
                    return "0001-01-01";
                }
            }
            else
                return "0001-01-01";
        }

        public string getDate2(object obj)
        {
            if (obj != null)
            {
                try
                {
                    return Convert.ToDateTime(obj).ToString("yyyy-MM-ddThh:mm:ss");
                }
                catch
                {
                    return "0001-01-01T00:00:00";
                }
            }
            else
                return "0001-01-01T00:00:00";

        }
        #endregion
        /// <summary>
        /// 写省系统到部系统同步记录
        /// </summary>
        /// <param name="iid">业务编号</param>
        /// <param name="type">同步类型 1：计划  2：实施 3：验收</param>
        /// <param name="way">同步方式 1：上传 2：下载</param>
        private void WriteS2BSYN_RECORD(string iid, string type, string way)
        {
            try
            {
                string strSql = "select count(1) from UT_S2BSYN_RECORD t where t.iid='" + iid + "'";
                int i = db_tdzzyw.Single<int>(strSql);
                if (i > 0)
                {
                    strSql = "update UT_S2BSYN_RECORD set {0}=1 where iid='{1}'";
                }
                else
                {
                    strSql = "insert into UT_S2BSYN_RECORD(iid,{0}) values('{1}',1) ";
                }

                string colname = "";

                switch (type)
                {
                    case "1":
                        colname = (way == "1" ? "JHUPLOAD" : "JHDOWNLOAD");
                        break;
                    case "2":
                        colname = (way == "1" ? "SSUPLOAD" : "SSDOWNLOAD");
                        break;
                    case "3":
                        colname = (way == "1" ? "YSUPLOAD" : "YSDOWNLOAD");
                        break;
                    case "4":
                        colname = (way == "1" ? "GBUPLOAD" : "GBDOWNLOAD");
                        break;
                }

                strSql = string.Format(strSql, colname, iid);

                db_tdzzyw.Execute(strSql);
            }
            catch (Exception ex)
            {
                ExceptionLog.WriteExceptionLog(ex);
                throw new Exception("写入同步记录失败！" + ex.Message);
            }
        }
        #region 上传失败处理
        /// <summary>
        /// 设置对应项目的对应阶段的状态为3即代表上传失败
        /// </summary>
        /// <param name="iid"></param>
        /// <param name="type"></param>
        private string FailToUp(string iid, string type)
        {
            string sqlstr = string.Format("");
            string result = "";
            try
            {
                string colname = "";
                switch (type)
                {
                    case "1":
                        colname = "JHUPLOAD";
                        break;
                    case "2":
                        colname = "SSUPLOAD";
                        break;
                    case "3":
                        colname = "YSUPLOAD";
                        break;
                    case "4":
                        colname = "GBUPLOAD";
                        break;
                }
                string strSql = "select count(1) from UT_S2BSYN_RECORD t where t.iid='" + iid + "' and" + " t.{0}=2";
                strSql = string.Format(strSql, colname, iid);
                int i = db_tdzzyw.Single<int>(strSql);
                if (i > 0)
                {
                    strSql = "update UT_S2BSYN_RECORD set {0}=3 where iid='{1}'";
                }
                else
                {
                    strSql = "insert into UT_S2BSYN_RECORD(iid,{0}) values('{1}',3) ";
                }

                strSql = string.Format(strSql, colname, iid);

                db_tdzzyw.Execute(strSql);
                result = "项目移出待办箱成功！";
            }
            catch (Exception ex)
            {
                ExceptionLog.WriteExceptionLog(ex);
                throw new Exception("项目移出待办箱失败！" + ex.Message);
            }
            return result;
        }
        #endregion
    }
}

